package Logic;

import Core.NetData;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import microgame.airplane.proto.MsgTypeOuterClass;
import microgame.airplane.proto.ProtoPacker;
import io.netty.channel.Channel;
import microgame.toolset.datatable.DataTable;
import microgame.toolset.datatable.DataTableManager;
import microgame.toolset.datatable.Row;

import java.util.Random;

public class Util {
    public final static int SCREENWIDTH = 1080;
    public final static int SCREENHEIGHT = 1920;

    private static Random random = new Random();


    public static boolean CheckLogon(NetData nd) {
        return (ClientManager.Instance().GetClient(nd.GetClientId()) != null);
    }

    public static void SendMessageThruChannel(int msgId, Channel channel, byte[] content) {
        byte[] response = ProtoPacker.PackMessage(msgId, content);
        channel.writeAndFlush(new BinaryWebSocketFrame(Unpooled.copiedBuffer(response)));
    }

    public static void GetMapCorners(Integer key, Quad corner) {
        DataTable mapTable = DataTableManager.Instance().GetDataTable("map");
        Row mapRow = mapTable.GetRow(key);
        int originX = mapRow.GetInteger("originx");
        int originY = mapRow.GetInteger("originx");
        int width = mapRow.GetInteger("width");
        int height = mapRow.GetInteger("height");
        width *= SCREENWIDTH;
        height *= SCREENHEIGHT;

        corner.xMin = originX - width / 2;
        corner.xMax = originX + width / 2;
        corner.yMin = originY - height / 2;
        corner.yMax = originY + height / 2;
    }

    public static float GetRandomFloat() {
        return random.nextFloat();
    }
}
